import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_train.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/train/model/train_model.dart';
import 'package:flutter_learn_vi/src/page/train/train_details_page.dart';
import 'package:flutter_learn_vi/src/page/train/train_history_widget.dart';
import 'package:get/get.dart';

import 'controller/train_tab_details1_controller.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/16.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///培训详情页面一
class TrainTabDetails1Page extends StatefulWidget {
  //培训ID
  final TrainApproveBean approveBean;

  const TrainTabDetails1Page({Key key, this.approveBean}) : super(key: key);

  @override
  _TrainTabDetails1PageState createState() => _TrainTabDetails1PageState();
}

class _TrainTabDetails1PageState extends State<TrainTabDetails1Page>
    with AutomaticKeepAliveClientMixin, TrainModel {
  @override
  bool get wantKeepAlive => true;
  TrainTabDetails1Controller trainTabDetails1Controller;

  @override
  void initState() {
    super.initState();
    trainTabDetails1Controller = Get.put(TrainTabDetails1Controller(),
        tag: widget.approveBean.trainId.toString());
    //查询用户是否参与培训
    getUserSelectTrain();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "培训详情",
          style: TextStyle(fontSize: 18),
        ),
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: RefreshIndicator(
          onRefresh: () async {
            await getUserSelectTrain();
          },
          child: buildMainClumn(),
        ),
      ),
    );
  }

  Widget buildMainClumn() {
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    if (_trainBean == null) {
      return Center(
        child: NoDataWidget(),
      );
    }
    /**
     * <el-radio :label="0">待发布</el-radio>
     * <el-radio :label="1">禁用</el-radio>
     * <el-radio :label="2">删除</el-radio>
     * <el-radio :label="9">未开始</el-radio>
     * <el-radio :label="5">进行中</el-radio>
     * <el-radio :label="6">已完成</el-radio>
     * train_status
     */
    int trainStatus = _trainBean.trainStatus;
    if (trainStatus == 0) {
      return Center(
        child: Text("培训还未发布 暂不可查看 "),
      );
    }
    if (trainStatus == 1) {
      return Center(
        child: Text(
          "培训已被禁用",
          style: TextStyle(color: Colors.red),
        ),
      );
    }
    if (trainStatus == 2) {
      return Center(
        child: Text(
          "培训已删除",
          style: TextStyle(color: Colors.red),
        ),
      );
    }
    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Column(
              children: [
                TrainDetailsPage(
                  trainBean: _trainBean,
                  approveBean: widget.approveBean,
                  isScaffold: false,
                  userSelectTrainBena: _userSelectTrainBena,
                ),
                buildTrainTeacherStartWidget()
              ],
            ),
          ),
        ),
        buildAddStudyWidget(),
      ],
    );
  }

  //用户是否参与了培训
  TrainBean _userSelectTrainBena;
  TrainBean _trainBean;
  bool _isLoading = true;

  Widget buildAddStudyWidget() {
    if (_userSelectTrainBena == null) {
      if (_isLoading) {
        return LoadingWidget();
      }
      return GetBuilder(
        id: "root",
        tag: widget.approveBean.trainId.toString(),
        builder: (TrainTabDetails1Controller controller) {
          if (_trainBean.trainStatus != 5) {
            return SizedBox();
          }
          return selectTrainStudy("立即加入学习");
        },
      );
    }
    return SizedBox();
  }

  Widget buildTrainTeacherStartWidget() {
    UserBean userBean = UserHelper.getInstance.userBean;

    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }

    if (_userSelectTrainBena != null) {
      //5 进行中 ；6已完成
      int trainStatus = _userSelectTrainBena.trainStatus;
      if (trainStatus == 6) {
        int userType = userBean.userType;
        if (userType != 12) {}

        return ElevatedButton(
          style: ButtonStyle(
            backgroundColor: MaterialStateProperty.resolveWith((states) {
              //设置按下时的背景颜色
              //默认不使用背景颜色
              return Colors.grey;
            }),
          ),
          onPressed: () {},
          child: Text("已完成该培训"),
        );
      }
    }

    ////用户未参与培训后显示加入学习
    if (_userSelectTrainBena == null) {
      return SizedBox();
    }
    String trainLine = _userSelectTrainBena.trainLine;
    if (trainLine == "offline") {
      //trainLineStr = "线下";
      //用户参与培训后显示签到
      //学生
      //return SizedBox();
      // return TrainStudentStartWidget(
      //   approveBean: widget.approveBean,
      // );
      return TrainHistoryWidget(
        approveBean: widget.approveBean,
        shrinkWrap: true,
        tag: "main",
      );
    } else {
      //trainLineStr = "线上课程学习";
      return TrainHistoryWidget(
        approveBean: widget.approveBean,
        shrinkWrap: true,
        tag: "main",
      );
    }
  }

  //查询用户是否参与培训
  Future<bool> getUserSelectTrain() async {
    _trainBean = await getTrainNetDatils(trainId: widget.approveBean.trainId);
    _userSelectTrainBena = await querySelectTrain(widget.approveBean.trainId);
    if (mounted) {
      setState(() {});
    }
    _isLoading = false;
    return true;
  }

  Widget selectTrainStudy(String title) {
    return ElevatedButton(
      onPressed: () async {
        setState(() {
          _isLoading = true;
        });
        _userSelectTrainBena =
            await selectStudyTrain(widget.approveBean.trainId);
        await getUserSelectTrain();
        _isLoading = false;
        if (mounted) {
          setState(() {});
        }
      },
      child: Text(title),
    );
  }
}
